package com.cqggrw.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.cqggrw.domain.Appraise;
import com.cqggrw.util.DBUtil;
import com.cqggrw.util.Utils;

public class AppraiseDaoImpl implements com.cqggrw.dao.AppraiseDaoI {

	public boolean addAppraise(Appraise t) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "insert into tbl_appraise(newsId, message) values(?, ?);";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, t.getNewsId());
			ps.setString(2, t.getMessage());
			i = ps.executeUpdate();
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

	public Appraise getById(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Appraise t  = null;
		
		String sql = "select a.*, b.name newsName from tbl_appraise a, tbl_news b where a.newsId = b.id and a.id = ?;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				t = new Appraise();
				t.setId(id);
				t.setMessage(rs.getString("message"));
				t.setNewsName(rs.getString("newsName"));
			}
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return t;
	}

	public List<Appraise> getByNews(Appraise t) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Appraise> getList(Appraise t) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Appraise appr = null;
		List<Appraise> list = new ArrayList<Appraise>();
		
		String sql = "select a.*, b.name newsName from tbl_appraise a, tbl_news b where a.newsId = b.id order by id desc limit ?, ?;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (t.getPage()-1)*t.getRows());
			ps.setInt(2, t.getRows());
			rs = ps.executeQuery();
			while (rs.next()) {
				appr = new Appraise();
				appr.setId(rs.getInt("id"));
				appr.setNewsId(rs.getInt("newsId"));
				appr.setMessage(rs.getString("message"));
				appr.setNewsName(rs.getString("newsName"));
				list.add(appr);
			}
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}

	public int getCount() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int total = 0;
		
		String sql = "select count(*) from tbl_appraise";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			if (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return total;
	}

	public boolean delete(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "delete from tbl_appraise where id = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

}
